#coding=utf-8
# Copyright 2011-2012 Maxim Petrov <maximpetrov@yahoo.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 3 or any later version.

from modal_window import ModalWindow
from controls import TextField, PasswordField, Button, Label
import curses.ascii

class PromptWindow(ModalWindow):
    """Modal window with prompt"""

    # Redefine ModalWindow constants
    _SHOULD_ENLARGE = False
    _PREFERRED_MIN_HEIGHT = 8
    _MIN_HEIGHT = 8
    _message_visible = True
    _EXIT_BUTTON = u'Cancel'
    _title = _(u'Prompt')
    __MIN_FIELD_WIDTH = 8
    __PREFERRED_FIELD_WIDTH = 16

    _OK = _(u'OK')
    _CANCEL = _(u'Cancel')
    _field_type = TextField

    def __init__(self, prompt):
        """
        Constructor.

        Keyword arguments:
            prompt - prompt for requested value
            field_type - class for input field
        """

        self.__prompt = prompt
        # Text field
        self.__field = self._field_type(None, lambda w, h: (2, 4, w - 4, None))
        # Buttons
        self.__ok_button = Button(None, lambda w, h: \
            (w / 4 - len(PromptWindow._OK) / 2, 6, None, None),
            self._OK)
        self.__cancel_button = Button(None, lambda w, h: \
            (w * 3 / 4 - len(PromptWindow._CANCEL) / 2 - 1, 6, None, None),
            self._CANCEL)
        self.__prompt_label = Label(None, lambda w, h: (2, 2, w - 4, None),
            self.__prompt)
        self._controls = [(self.__field, True), (self.__prompt_label, False),
                          (self.__ok_button, True), (self.__cancel_button, True)]
        buttons_width = (max(len(PromptWindow._OK), len(PromptWindow._CANCEL)) +\
                        4) * 2
        self._PREFERRED_MIN_WIDTH = max(len(prompt) + 2, buttons_width,
                                        self.__PREFERRED_FIELD_WIDTH + 2) + 2
        self._MIN_WIDTH = max(len(prompt) + 2, buttons_width,
                                        self.__MIN_FIELD_WIDTH + 2) + 2

    def handle_keypress(self, ch, uni):
        """
        Function handle keypress.

        Keywords arguments:
            ch - pressed key
            uni - True if ch is Unicode char, False otherwise

        Return value:
            Return dictionary contains pressed button if this keypress results
            closing window, otherwise return None
        """

        result = ModalWindow.handle_keypress(self, ch, uni)
        if result is not None:
            return result
        if ch == ord('\n'):
            if self._selected == self.__cancel_button:
                return {u'button': u'Cancel'}
            else:
                return {u'button': u'OK',
                        u'value': self.__field.text}
        elif ch == ord('\t'):
            self._next_control()
        else:
            self._selected.handle_keypress(ch, uni)

class PasswordPromptWindow(PromptWindow):
    """Modal window with password prompt"""

    _title = _(u'Password prompt')

    _field_type = PasswordField
